package com.yy.ant.chat.controller;


import com.yy.ant.sys.service.IUserService;
import com.yy.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yy.ant.chat.service.IChatmessageService;
import com.yy.ant.sys.entity.User;
import com.yy.model.ResultMsg;
import com.yy.utils.SessionUtil;

/**
 * <p>
 * 聊天记录表 前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/chat/chatmessage")
public class ChatmessageController {

	@Autowired
	private IChatmessageService iChatmessageService;
	@Autowired
	private IUserService iUserService;
	/**
	 * 根据用户ID 初始化群组数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getMsg")
	public ResultMsg getMsg(@RequestHeader("token") String token,@RequestParam(value = "id") String id, @RequestParam(value = "type") String type) {
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		int types=0;
		if (StringUtils.equalsIgnoreCase("friend", type)) {
			types=1;
		}else {
			types=2;
		}
		return iChatmessageService.unreadMsgCount(id, sysUser.getId().toString(),types);
	}

}
